package com.gylang.print.pdf.domain;

import com.gylang.print.pdf.domain.component.BasePdfComponent;
import com.gylang.print.pdf.domain.component.img.ImgComponent;
import com.gylang.print.pdf.enums.ContentMode;
import lombok.Builder;
import lombok.Data;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gylang
 * data 2021/6/4
 */
@Data
@Builder(toBuilder = true)
public class PdfPageWrapper {

	public static final float A4_WIDTH = 595.28F;
	public static final float A4_HEIGHT = 842.28F;

	private PdfWrapper pdfWrapper;

	/** pdf 数据 */
	@Builder.Default
	private List<BasePdfComponent> pdfComponents = new ArrayList<>();

	/** 背景 */
	private ImgComponent background;
	/** 样式 */
	@Builder.Default
	private PdfStyle style = new PdfStyle();
	/** 页面 */
	private PDPage page;

	/** x轴左外边距 */
	private float marginLeftX;

	/** x轴右外边距 */
	private float marginRightX;

	/** y轴顶部外边距 */
	private float marginTopY;

	/** y轴底部外边距 */
	private float marginBottomY;

	/** 操作流 */
	private PDPageContentStream contentStream;


	// 样色 ---------------------------------------------------------------


	/** 宽度 */
	@Builder.Default
	private float width = A4_WIDTH;

	/** 长度 */
	@Builder.Default
	private float height = A4_HEIGHT;

	/** 颜色 */
	private Color color;


	/** 写入模式 */
	@Builder.Default
	private ContentMode contentMode = ContentMode.APPEND;


	public PDPageContentStream getContentStream() {
		if (null == contentStream) {
			try {
				contentStream = new PDPageContentStream(
					getPdfWrapper().getDocument(),
					getPage(),
					getContentMode().getAppendMode(),
					true,
					false
				);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return contentStream;
	}

	public PDPageContentStream createContentStream() {
		try {
			return new PDPageContentStream(
				getPdfWrapper().getDocument(),
				getPage(),
				getContentMode().getAppendMode(),
				true,
				false
			);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public PdfPageWrapper addComponent(BasePdfComponent component) {
		pdfComponents.add(component);
		return this;
	}
}
